package JavaSe;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.function.Consumer;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}9:30
 */
public class ArrayListDemo<T> implements Iterator<T> {

    private T[] theItems;
    private int theSize;
    private static final int DEAULT_CAPACITY = 10;

    public ArrayListDemo() {
        theSize = 0;

    }

    public void add(T data){
        if (size()==theItems.length){
            ensureCapacity(size()*2+1);
        }
        theItems[size()]=data;
        theSize++;
    }

    public void add(int index,T data){
        if (size()==theItems.length){
            ensureCapacity(size()*2+1);
        }
        for (int i = theSize; i > index; i--){
            theItems[i]=theItems[i-1];
        }
        theItems[index] = data;
        theSize++;
    }
    public T get(int index){
        if (index < 0 || index >= size()){
            throw new IndexOutOfBoundsException("index error");
        }
        return theItems[index];
    }

    public T remove(int index){
        T removeData = get(index);
        for (int i = index; i < size() - 1; i++){
            theItems[i] = theItems[i+1];
        }
        theSize--;
        return removeData;
    }

    private void ensureCapacity(int newCapacity){
        if (theSize>newCapacity) {
            return;
        }

        T[] old = theItems;
        theItems = (T[]) new Object[newCapacity];
        for (int i = 0; i < size(); i++){
            theItems[i] = old[i];
        }
    }

    public int size(){
        return theSize;
    }

    @Override
    public boolean hasNext() {
        return false;
    }

    @Override
    public T next() {
        return null;
    }

    @Override
    public void remove() {

    }

    @Override
    public void forEachRemaining(Consumer<? super T> action) {

    }
}
